package com.manage.design.controller;

import com.manage.common.resultUtils.JsonResult;
import com.manage.common.resultUtils.Md5Result;
import com.manage.common.enums.ResultEnum;
import com.manage.design.dto.OperatorInfo;
import com.manage.design.pojo.Operator;
import com.manage.design.service.OperatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


/**
 * @Create by ldr.
 * @create 2018-09-07 13:49
 * @Description：操作员控制器
 */
@Controller
@RequestMapping(value = "/user")
public class OperatorController {

    private final static Logger logger = LoggerFactory.getLogger(OperatorController.class);

    @Autowired
    private OperatorService operatorService;

    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    /**
     * description:登陆
     * @params [operatorInfo] 操作员信息
     * @return com.manage.common.resultUtils.JsonResult<com.manage.design.pojo.Operator>
     */
    @PostMapping(value = "/subLogin", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult<Operator> subLogin(@RequestBody Operator operatorInfo,HttpSession session) {

        //根据账号查询操作员信息
        Operator operator = operatorService.getOperatorByUsername(operatorInfo.getUsername());

        if(operator != null){

            if (Md5Result.verify(operatorInfo.getPassword(),operator.getPassword())) {
                logger.info("登录成功 operator = {}", operator);
                session.setAttribute("user",operatorInfo);
                return new JsonResult<Operator>(ResultEnum.SUCCESS);
            } else {
                logger.warn("密码错误 operator = {}", operator);
                return new JsonResult<Operator>(ResultEnum.FAILURE);
            }

        }else {
            logger.warn("没有该用户 operator = {}", operator);
            return new JsonResult<Operator>(ResultEnum.ERROR);
        }
    }

    /*
     * description:修改密码
     * @params [info, result] 修改密码封装类  表单验证
     * @return com.manage.common.resultUtils.JsonResult<com.manage.design.pojo.Operator>
     */
    @PostMapping(value = "/update",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult<Operator> updatePassword(@Valid @RequestBody OperatorInfo info,BindingResult result){

        //表单验证
        if (result.hasErrors()){
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                logger.error("信息不能为空 error = {}",error);
            }
            return new JsonResult<Operator>(ResultEnum.ERROR);
        }
        //根据账号查询操作员信息
        Operator operator = operatorService.getOperatorByUsername(info.getUser());
        if (operator != null && info.getPassNew().equals(info.getPassNewOk())){

            if (Md5Result.verify(info.getPassOld(),operator.getPassword())) {
                operator.setPassword(Md5Result.generate(info.getPassNew()));
                //更改密码
                int record = operatorService.updateByUsername(operator);
                logger.info("验证成功 record = {}", record);

                return new JsonResult<Operator>(ResultEnum.SUCCESS);
            } else {
                logger.warn("验证失败 operator = {}", operator);
                return new JsonResult<Operator>(ResultEnum.FAILURE);
            }
        }
        logger.warn("没有该用户 operator = {}", operator);
        return new JsonResult<Operator>(ResultEnum.FAILURE);
    }

    /*
     * description:退出 移除session
     * @params [session]
     * @return java.lang.String
     */
    @GetMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/user/login";
    }

}
